package com.school.empsys.controller;

import java.util.List;

import com.google.common.collect.Maps;
import com.school.empsys.model.Role;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.school.empsys.mapper.UserMapper;
import com.school.empsys.dto.UserDto;
import com.school.empsys.model.User;
import com.school.empsys.page.table.PageTableRequest;
import com.school.empsys.page.table.PageTableHandler;
import com.school.empsys.page.table.PageTableResponse;
import com.school.empsys.page.table.PageTableHandler.CountHandler;
import com.school.empsys.page.table.PageTableHandler.ListHandler;
import com.school.empsys.service.UserService;
import com.school.empsys.utils.UserUtil;


/**
 * 用户相关接口
 */
@RestController
@RequestMapping("/users")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userDao;

    /**
     * 保存用户
     *
     * @param userDto
     * @return
     */
    @PostMapping
    @RequiresPermissions("sys:user:add")
    public User saveUser(@RequestBody UserDto userDto) {
        User u = userService.getUser(userDto.getUsername());
        if (u != null) {
            throw new IllegalArgumentException(userDto.getUsername() + "已存在");
        }
        return userService.saveUser(userDto);
    }

    /**
     * 修改用户
     *
     * @param userDto
     * @return
     */
    @PutMapping
    @RequiresPermissions("sys:user:update")
    public User updateUser(@RequestBody UserDto userDto) {
        return userService.updateUser(userDto);
    }

    /**
     * 修改头像
     *
     * @param headImgUrl
     */
    @PutMapping(params = "headImgUrl")
    public void updateHeadImgUrl(String headImgUrl) {
        User user = UserUtil.getCurrentUser();
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);
        userDto.setHeadImgUrl(headImgUrl);

        userService.updateUser(userDto);
        log.debug("{}修改了头像", user.getUsername());
    }

    /**
     * 修改密码
     *
     * @param username
     * @param oldPassword
     * @param newPassword
     */
    @PutMapping("/{username}")
    @RequiresPermissions("sys:user:update")
    public void changePassword(@PathVariable String username, String oldPassword, String newPassword) {
        userService.changePassword(username, oldPassword, newPassword);
    }

    /**
     * 用户列表
     *
     * @param request
     * @return
     */
    @GetMapping
    @RequiresPermissions("sys:user:query")
    public PageTableResponse listUsers(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {
            @Override
            public int count(PageTableRequest request) {
                return userDao.count(request.getParams());
            }
        }, new ListHandler() {
            @Override
            public List<User> list(PageTableRequest request) {
                List<User> list = userDao.list(request.getParams(), request.getOffset(), request.getLimit());
                return list;
            }
        }).handle(request);
    }

    /**
     * 当前登录用户
     *
     * @return
     */
    @GetMapping("/current")
    public User currentUser() {
        return UserUtil.getCurrentUser();
    }

    /**
     * 根据用户id获取用户
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @RequiresPermissions("sys:user:query")
    public User user(@PathVariable Long id) {
        return userDao.getById(id);
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("sys:user:add")
    public void delete(@PathVariable Long id) {
        userService.deleteById(id);
    }

    @GetMapping("/listEmps")
    @RequiresPermissions("sys:user:query")
    public List<User> listEmps() {
        return userService.listEmps();
    }
}
